package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentIntellpropBrandBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseFragComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseFragModule;
import com.sgb.kjwl.model.entity.enterprise.CopyrightListEntity2;
import com.sgb.kjwl.model.entity.enterprise.IntellpropEntity;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.PatentEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.PickPriceUtils;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.event.SearchEvent;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverDetailActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.IntellpropAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.IpCopyrightAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.PatentAdapter;
import com.sgb.kjwl.view.widget.IpPopWindow;
import com.sgb.kjwl.view.widget.MakeOverMoreFilterPopWindow;
import com.sgb.kjwl.viewmodel.enterprise.IntellpropFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class IntellpropFragment extends AppFragment {
    @Inject
    IntellpropFragModel model;

    private FragmentIntellpropBrandBinding binding;

    private IpPopWindow brandPopWindow;//商标分类
    private IpPopWindow pricePopWindow1;//出售价格-商标
    private IpPopWindow makeupPopWindow;//组合类型
    private IpPopWindow lengthPopWindow;//字符长度
    private IpPopWindow copyrightPopWindow;//商标分类
    private IpPopWindow pricePopWindow2;//出售价格-版权
    private IpPopWindow onlyPopWindow;//是否独家
    private IpPopWindow tradePopWindow;//行业分类
    private IpPopWindow patentTypePopWindow;//专利类型
    private IpPopWindow patentStatePopWindow;//专利状态
    private IpPopWindow pricePopWindow3;//出售价格-专利

    private MakeOverMoreFilterPopWindow tradeMoreWindow;
    private MakeOverMoreFilterPopWindow patentMoreWindow;

    private List<IpPickSelectEntity> brandDatas;
    private List<IpPickSelectEntity> priceDatas1;
    private List<IpPickSelectEntity> makeupDatas;
    private List<IpPickSelectEntity> lengthDatas;
    private List<IpPickSelectEntity> copyrightDatas;
    private List<IpPickSelectEntity> priceDatas2;
    private List<IpPickSelectEntity> onlyDatas;
    private List<IpPickSelectEntity> tradeDatas;
    private List<IpPickSelectEntity> patentTypeDatas;
    private List<IpPickSelectEntity> patentStateDatas;
    private List<IpPickSelectEntity> priceDatas3;

    private List<IpPickSelectEntity> priceList = new ArrayList<>();//转让价格
    private List<IpPickSelectEntity> publishTypeList = new ArrayList<>();//发布类型

    private Map<String, Object> map;
    private Map<String, Object> mapCopyright;
    private Map<String, Object> mapPatent;

    private boolean isChange1;
    private boolean isChange2;
    private boolean isChange3;
    private boolean isChange4;
    private boolean isChange5;
    private boolean isChange6;
    private boolean isChange7;
    private boolean isChange8;
    private boolean isChange9;
    private boolean isChange10;
    private boolean isChange11;

    private IntellpropAdapter intellpropAdapter;
    private PatentAdapter patentAdapter;
    private IpCopyrightAdapter ipCopyrightAdapter;
    private int position = -1;
    private String category = "";

    public static IntellpropFragment newInstance(int position, String category) {
        IntellpropFragment fragment = new IntellpropFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putString("category", category);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 個人企業團隊網站
     *
     * @param position
     * @param category
     * @param websiteNo
     * @param websiteType
     * @return
     */
    public static IntellpropFragment newInstance(int position, String category, String websiteNo, String websiteType) {
        IntellpropFragment fragment = new IntellpropFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putString("category", category);
        bundle.putString("websiteNo", websiteNo);
        bundle.putString("websiteType", websiteType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerEnterpriseFragComponent.builder()
                .appComponent(appComponent)
                .enterpriseFragModule(new EnterpriseFragModule(this))
                .build()
                .inject(this);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_intellprop_brand, container, false);
        binding.setViewModel(model);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        model.websiteNo = getArguments().getString("websiteNo");
        model.websiteType = getArguments().getString("websiteType");
        position = getArguments().getInt("position");
        category = getArguments().getString("category");
        initView();
        initData();
        getData();
    }

    private void initTab() {
        switch (position) {
            case 0://商标
                binding.displayView.setAdapter(intellpropAdapter);
                binding.tvClass1.setText("商标分类");
                binding.tvClass2.setText("组合类型");
                binding.tvClass3.setText("字符长度");
                binding.tvMoreChoose.setVisibility(View.VISIBLE);
                intellpropAdapter.setOnItemClick(new AdapterOnItemClick<IntellpropEntity>() {
                    @Override
                    public void onItemClick(IntellpropEntity intellpropEntity, int p) {
                        ActivityJump.jumpToActivity(getActivity(), MakeOverDetailActivity.class, Pair.create("detail", "商标"), Pair.create("id", intellpropEntity.getId()));
                    }
                });
                break;
            case 1://版权
                binding.displayView.setAdapter(ipCopyrightAdapter);
                binding.tvClass1.setText("版权分类");
                binding.tvClass2.setText("转让价格");
                binding.tvClass3.setText("发布类型");
                if(!TextUtils.isEmpty(model.websiteType)&&!TextUtils.isEmpty(model.websiteNo)) {
                    binding.tvClass3.setVisibility(View.GONE);
                }
                binding.tvMoreChoose.setVisibility(View.GONE);
                ipCopyrightAdapter.setOnItemClick(new AdapterOnItemClick<CopyrightListEntity2>() {
                    @Override
                    public void onItemClick(CopyrightListEntity2 copyrightListEntity2, int p) {
                        ActivityJump.jumpToActivity(getActivity(), MakeOverDetailActivity.class, Pair.create("detail", "版权"), Pair.create("id", copyrightListEntity2.getId()));
                    }
                });
                break;
            case 2://专利
                binding.displayView.setAdapter(patentAdapter);
                binding.tvClass1.setText("领域分类");
                binding.tvClass2.setText("专利类型");
                binding.tvClass3.setText("专利状态");
                binding.tvMoreChoose.setVisibility(View.VISIBLE);
                patentAdapter.setOnItemClick(new AdapterOnItemClick<PatentEntity>() {
                    @Override
                    public void onItemClick(PatentEntity patentEntity, int p) {
                        ActivityJump.jumpToActivity(getActivity(), MakeOverDetailActivity.class, Pair.create("detail", "专利"), Pair.create("id", patentEntity.getId()));
                    }
                });
                break;
        }
    }

    private void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        binding.tvClass1.setOnClickListener(this);
        binding.tvClass2.setOnClickListener(this);
        binding.tvClass3.setOnClickListener(this);
//        binding.tvClass4.setOnClickListener(this);
        binding.tvMoreChoose.setOnClickListener(this);
        if (patentAdapter == null) {
            patentAdapter = new PatentAdapter(getActivity(), null);
        }
        if (intellpropAdapter == null) {
            intellpropAdapter = new IntellpropAdapter(getActivity(), null);
        }
        if (ipCopyrightAdapter == null) {
            ipCopyrightAdapter = new IpCopyrightAdapter(getActivity(), null);
        }
        switch (position) {
            case 0:
                binding.displayView.setAdapter(intellpropAdapter);
                break;
            case 1:
                binding.displayView.setAdapter(ipCopyrightAdapter);
                break;
            case 2:
                binding.displayView.setAdapter(patentAdapter);
                break;
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.displayView.setLayoutManager(layoutManager);
        binding.displayView.setLoadingMoreEnabled(true);
        binding.displayView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getData();
            }

            @Override
            public void onLoadMore() {
                switch (position) {
                    case 0:
                        model.trademarkList(true, map);
                        break;
                    case 1:
                        model.getCopyrightList(true, mapCopyright);
                        break;
                    case 2:
                        model.getPatentList(true, mapPatent);
                        break;
                }
            }
        });
        initTab();

        tradeMoreWindow = new MakeOverMoreFilterPopWindow(getContext());
        tradeMoreWindow.setPublishTypeVisibility((!TextUtils.isEmpty(model.websiteNo)&&!TextUtils.isEmpty(model.websiteType))?View.GONE:View.VISIBLE);
        tradeMoreWindow.setOnResultCallBack(new BasePopWindow.OnResultCallBack<IpPickSelectEntity>() {
            @Override
            public void result(IpPickSelectEntity result) {
                if (null == result) {
                    resetAllChoose();
                } else {
                    map = PickPriceUtils.pickPrice(map, result);
                    //发布类型
                    map.put("publishType", result.getPublishType());
                    model.trademarkList(false, map);
                }
            }
        });
        tradeMoreWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tradeMoreWindow.resetWindow();
            }
        });

        patentMoreWindow = new MakeOverMoreFilterPopWindow(getContext());
        patentMoreWindow.setPublishTypeVisibility((!TextUtils.isEmpty(model.websiteNo)&&!TextUtils.isEmpty(model.websiteType))?View.GONE:View.VISIBLE);
        patentMoreWindow.setOnResultCallBack(new BasePopWindow.OnResultCallBack<IpPickSelectEntity>() {
            @Override
            public void result(IpPickSelectEntity result) {
                if (null == result) {
                    resetAllChoose();
                } else {
                    mapPatent = PickPriceUtils.pickPrice(mapPatent, result);
                    //发布类型
                    mapPatent.put("publishType", result.getPublishType());
                    model.getPatentList(false, mapPatent);
                }
            }
        });
        patentMoreWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                patentMoreWindow.resetWindow();
            }
        });

        brandPopWindow = new IpPopWindow(getActivity());
        brandPopWindow.setOnResultClick((type, key2, key3) -> {
            //商标名称
            map.put("categoryNo", ((IpPickSelectEntity) type).getValue());
            model.trademarkList(false, map);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange1 = true;
                setTextViewBg(binding.tvClass1, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange1 = false;
                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass1, 2);
            }
        });
        brandPopWindow.setOnDismissListener(() -> {
            if (!isChange1) {
                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass1, 2);
            } else {
                setDrwImage(binding.tvClass1, 1);
            }
        });
        pricePopWindow1 = new IpPopWindow(getActivity());
        pricePopWindow1.setOnResultClick((type, key2, key3) -> {
            map = PickPriceUtils.pickPrice(map, (IpPickSelectEntity) type);
            model.trademarkList(false, map);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange2 = true;
                setTextViewBg(binding.tvClass2, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange2 = false;
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
            }
        });
        pricePopWindow1.setOnDismissListener(() -> {
            if (!isChange2) {
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
            } else {
                setDrwImage(binding.tvClass2, 1);
            }
        });
        makeupPopWindow = new IpPopWindow(getActivity());
        makeupPopWindow.setOnResultClick((type, key2, key3) -> {
            //组合类型
            map.put("type", ((IpPickSelectEntity) type).getValue());
            model.trademarkList(false, map);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange3 = true;
                setTextViewBg(binding.tvClass2, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange3 = false;
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
            }
        });
        makeupPopWindow.setOnDismissListener(() -> {
            if (!isChange3) {
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
            } else {
                setDrwImage(binding.tvClass2, 1);
            }
        });
        lengthPopWindow = new IpPopWindow(getActivity());
        lengthPopWindow.setOnResultClick((type, key2, key3) -> {
            //最大长度
            map.put("maxLength", ((IpPickSelectEntity) type).getMaxLength());
            //最小长度
            map.put("minLength", ((IpPickSelectEntity) type).getMinLength());
            model.trademarkList(false, map);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange4 = true;
                setTextViewBg(binding.tvClass3, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange4 = false;
                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass3, 2);
            }
        });
        lengthPopWindow.setOnDismissListener(() -> {
            if (!isChange4) {
                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass3, 2);
            } else {
                setDrwImage(binding.tvClass3, 1);
            }
        });

        copyrightPopWindow = new IpPopWindow(getActivity());
        copyrightPopWindow.setOnResultClick((type, key2, key3) -> {
            //版权分类
            mapCopyright.put("category", ((IpPickSelectEntity) type).getValue());
            model.getCopyrightList(false, mapCopyright);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange5 = true;
                setTextViewBg(binding.tvClass1, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange5 = false;
                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass1, 2);
            }
        });
        copyrightPopWindow.setOnDismissListener(() -> {
            if (!isChange5) {
                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass1, 2);
            } else {
                setDrwImage(binding.tvClass1, 1);
            }
        });
        pricePopWindow2 = new IpPopWindow(getActivity());
        pricePopWindow2.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                mapCopyright = PickPriceUtils.pickPrice(mapCopyright, (IpPickSelectEntity) type);
                model.getCopyrightList(false, mapCopyright);
                if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                    isChange6 = true;
                    setTextViewBg(binding.tvClass2, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
                } else {
                    isChange6 = false;
                    setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                    setDrwImage(binding.tvClass2, 2);
                }
            }
        });
        pricePopWindow2.setOnDismissListener(() -> {
            if (!isChange6) {
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
            } else {
                setDrwImage(binding.tvClass2, 1);
            }
        });
        onlyPopWindow = new IpPopWindow(getActivity());
        onlyPopWindow.setOnResultClick((type, key2, key3) -> {
            //授权方式
            mapCopyright.put("publishType", ((IpPickSelectEntity) type).getPublishType());
            model.getCopyrightList(false, mapCopyright);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange7 = true;
                setTextViewBg(binding.tvClass3, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange7 = false;
                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass3, 2);
            }
        });
        onlyPopWindow.setOnDismissListener(() -> {
            if (!isChange7) {
                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass3, 2);
            } else {
                setDrwImage(binding.tvClass3, 1);
            }
        });
        tradePopWindow = new IpPopWindow(getActivity());
        tradePopWindow.setOnResultClick((type, key2, key3) -> {
            //行业分类
            mapPatent.put("industry", ((IpPickSelectEntity) type).getId());
            model.getPatentList(false, mapPatent);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange8 = true;
                setTextViewBg(binding.tvClass1, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange8 = false;
                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass1, 2);
            }
        });
        tradePopWindow.setOnDismissListener(() -> {
            if (!isChange8) {
                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass1, 2);
            } else {
                setDrwImage(binding.tvClass1, 1);
            }
        });
        patentTypePopWindow = new IpPopWindow(getActivity());
        patentTypePopWindow.setOnResultClick((type, key2, key3) -> {
            //专利类型
            mapPatent.put("categoryNo", ((IpPickSelectEntity) type).getId());
            model.getPatentList(false, mapPatent);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange9 = true;
                setTextViewBg(binding.tvClass2, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange9 = false;
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
            }
        });
        patentTypePopWindow.setOnDismissListener(() -> {
            if (!isChange9) {
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
            } else {
                setDrwImage(binding.tvClass2, 1);
            }
        });
        patentStatePopWindow = new IpPopWindow(getActivity());
        patentStatePopWindow.setOnResultClick((type, key2, key3) -> {
            //专利状态
            mapPatent.put("talentStatus", ((IpPickSelectEntity) type).getValue());
            model.getPatentList(false, mapPatent);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange10 = true;
                setTextViewBg(binding.tvClass3, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange10 = false;
                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass3, 2);
            }
        });
        patentStatePopWindow.setOnDismissListener(() -> {
            if (!isChange10) {
                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass3, 2);
            } else {
                setDrwImage(binding.tvClass3, 1);
            }
        });
        pricePopWindow3 = new IpPopWindow(getActivity());
        pricePopWindow3.setOnResultClick((type, key2, key3) -> {
            mapPatent = PickPriceUtils.pickPrice(mapPatent, (IpPickSelectEntity) type);
            model.getPatentList(false, mapPatent);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange11 = true;
                setTextViewBg(binding.tvClass4, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange11 = false;
                setTextViewBg(binding.tvClass4, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass4, 2);
            }
        });
        pricePopWindow3.setOnDismissListener(() -> {
            if (!isChange11) {
                setTextViewBg(binding.tvClass4, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass4, 2);
            } else {
                setDrwImage(binding.tvClass4, 1);
            }
        });


        brandPopWindow.setBgView(binding.grayLayout);
        pricePopWindow1.setBgView(binding.grayLayout);
        makeupPopWindow.setBgView(binding.grayLayout);
        lengthPopWindow.setBgView(binding.grayLayout);
        copyrightPopWindow.setBgView(binding.grayLayout);
        pricePopWindow2.setBgView(binding.grayLayout);
        onlyPopWindow.setBgView(binding.grayLayout);
        tradePopWindow.setBgView(binding.grayLayout);
        patentStatePopWindow.setBgView(binding.grayLayout);
        patentTypePopWindow.setBgView(binding.grayLayout);
        pricePopWindow3.setBgView(binding.grayLayout);

        brandPopWindow.setBackground(R.color.white);
        pricePopWindow1.setBackground(R.color.white);
        makeupPopWindow.setBackground(R.color.white);
        lengthPopWindow.setBackground(R.color.white);
        copyrightPopWindow.setBackground(R.color.white);
        pricePopWindow2.setBackground(R.color.white);
        onlyPopWindow.setBackground(R.color.white);
        tradePopWindow.setBackground(R.color.white);
        patentStatePopWindow.setBackground(R.color.white);
        patentTypePopWindow.setBackground(R.color.white);
        pricePopWindow3.setBackground(R.color.white);

    }

    private void initData() {
        map = new HashMap<>();
        mapCopyright = new HashMap<>();
        mapPatent = new HashMap<>();
    }

    /**
     * 获取列表数据
     */
    private void getData() {
        switch (position) {
            case 0: // 商标
                if (!TextUtils.isEmpty(category)) {
                    map.put("categoryNo", category);
                    setTextViewBg(binding.tvClass1, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
                    isChange1 = true;
                }
                model.getData(position, map);
                break;
            case 1: //版权
                if (!TextUtils.isEmpty(category)) {
                    mapCopyright.put("category", category);
                    setTextViewBg(binding.tvClass1, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
                    isChange5 = true;
                }
                model.getData(position, mapCopyright);
                break;
            case 2: //专利
                if (!TextUtils.isEmpty(category)) {
                    mapPatent.put("categoryNo", category);
                    setTextViewBg(binding.tvClass2, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
                    isChange9 = true;
                }
                model.getData(position, mapPatent);
                break;
        }
    }

    /**
     * 还原所有筛选项
     */
    private void resetAllChoose() {
        switch (position) {
            case 0: // 商标
                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass1, 2);
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass3, 2);
                isChange1 = false;
                isChange2 = false;
                isChange3 = false;
                isChange4 = false;
                brandPopWindow.setDefItem(0);
                makeupPopWindow.setDefItem(0);
                lengthPopWindow.setDefItem(0);
                map.clear();
                model.getData(position, map);
                break;
            case 2: //专利
                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass1, 2);
                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass2, 2);
                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
                setDrwImage(binding.tvClass3, 2);
                isChange8 = false;
                isChange9 = false;
                isChange10 = false;
                isChange11 = false;
                tradePopWindow.setDefItem(0);
                patentTypePopWindow.setDefItem(0);
                patentStatePopWindow.setDefItem(0);
                mapPatent.clear();
                model.getData(position, mapPatent);
                break;
        }
    }

    /**
     * 商标数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showList(List<IntellpropEntity> list, boolean isLoadMore) {
        binding.displayView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.displayView.loadMoreComplete();
                intellpropAdapter.addDatas(list);
            } else {
                binding.displayView.setNoMore(true);
            }
            binding.displayView.setVisibility(View.VISIBLE);
        } else {
            binding.displayView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.displayView.setVisibility(View.VISIBLE);
                binding.errorView.setVisibility(View.GONE);
                intellpropAdapter.setDatas(list);
                binding.displayView.setLoadingMoreEnabled(true);
            } else {
                intellpropAdapter.setDatas(new ArrayList<>());
                binding.displayView.setVisibility((!TextUtils.isEmpty(model.websiteType)&&!TextUtils.isEmpty(model.websiteNo))?View.VISIBLE:View.GONE);
                binding.grayLayout.setVisibility(View.GONE);
                binding.errorView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 专利数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showList2(List<PatentEntity> list, boolean isLoadMore) {
        binding.displayView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.displayView.loadMoreComplete();
                patentAdapter.addDatas(list);
            } else {
                binding.displayView.setNoMore(true);
            }
            binding.displayView.setVisibility(View.VISIBLE);
        } else {
            binding.displayView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.displayView.setVisibility(View.VISIBLE);
                binding.errorView.setVisibility(View.GONE);
                patentAdapter.setDatas(list);
                binding.displayView.setLoadingMoreEnabled(true);
            } else {
                patentAdapter.setDatas(new ArrayList<>());
                binding.displayView.setVisibility((!TextUtils.isEmpty(model.websiteType)&&!TextUtils.isEmpty(model.websiteNo))?View.VISIBLE:View.GONE);
                binding.grayLayout.setVisibility(View.GONE);
                binding.errorView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 版本数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showCopyrightList(List<CopyrightListEntity2> list, boolean isLoadMore) {
        binding.displayView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.displayView.loadMoreComplete();
                ipCopyrightAdapter.addDatas(list);
            } else {
                binding.displayView.setNoMore(true);
            }
            binding.displayView.setVisibility(View.VISIBLE);
        } else {
            binding.displayView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.displayView.setVisibility(View.VISIBLE);
                binding.errorView.setVisibility(View.GONE);
                ipCopyrightAdapter.setDatas(list);
                binding.displayView.setLoadingMoreEnabled(true);
            } else {
                ipCopyrightAdapter.setDatas(new ArrayList<>());
                binding.displayView.setVisibility((!TextUtils.isEmpty(model.websiteType)&&!TextUtils.isEmpty(model.websiteNo))?View.VISIBLE:View.GONE);
                binding.grayLayout.setVisibility(View.GONE);
                binding.errorView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_class1:
                switch (position) {
                    case 0:
                        if (!isChange1) {
                            if (!brandPopWindow.isShowing()) {
                                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass1, 0);
                            }
                        } else {
                            if (!brandPopWindow.isShowing()) {
                                setDrwImage(binding.tvClass1, 0);
                            }
                        }
                        if (null != brandDatas && brandDatas.size() > 0) {
                            brandPopWindow.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getTrademarkTypeOption();
                        }
                        break;
                    case 1:
                        if (!isChange5) {
                            if (!copyrightPopWindow.isShowing()) {
                                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass1, 0);
                            }
                        } else {
                            if (!copyrightPopWindow.isShowing()) {
                                setDrwImage(binding.tvClass1, 0);
                            }
                        }
                        if (null != copyrightDatas && copyrightDatas.size() > 0) {
                            copyrightPopWindow.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getCopyrightTypeOption();
                        }
                        break;
                    case 2:
                        if (!isChange8) {
                            if (!tradePopWindow.isShowing()) {
                                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass1, 0);
                            }
                        } else {
                            if (!tradePopWindow.isShowing()) {
                                setDrwImage(binding.tvClass1, 0);
                            }
                        }
                        if (null != tradeDatas && tradeDatas.size() > 0) {
                            tradePopWindow.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.queryTerritoryType();
                        }
                        break;
                }
                break;
            case R.id.tv_class2:
                switch (position) {
                    case 0:
                        if (!isChange3) {
                            if (!makeupPopWindow.isShowing()) {
                                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass2, 0);
                            }
                        } else {
                            if (!makeupPopWindow.isShowing()) {
                                setDrwImage(binding.tvClass2, 0);
                            }
                        }
                        if (null != makeupDatas && makeupDatas.size() > 0) {
                            makeupPopWindow.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getTypeOption();
                        }
                        break;
                    case 1:
                        if (!isChange6) {
                            if (!pricePopWindow2.isShowing()) {
                                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass2, 0);
                            }
                        } else {
                            if (!pricePopWindow2.isShowing()) {
                                setDrwImage(binding.tvClass2, 0);
                            }
                        }
                        if (null != priceDatas2 && priceDatas2.size() > 0) {
                            pricePopWindow2.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getPriceOptionByNo(6);
                        }
                        break;
                    case 2:
                        if (!isChange9) {
                            if (!patentTypePopWindow.isShowing()) {
                                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass2, 0);
                            }
                        } else {
                            if (!patentTypePopWindow.isShowing()) {
                                setDrwImage(binding.tvClass2, 0);
                            }
                        }
                        if (null != patentTypeDatas && patentTypeDatas.size() > 0) {
                            patentTypePopWindow.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getPatentTypeOption();
                        }
                        break;
                }
                break;
            case R.id.tv_class3:
                switch (position) {
                    case 0:
                        if (!isChange4) {
                            if (!lengthPopWindow.isShowing()) {
                                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass3, 0);
                            }
                        } else {
                            if (!lengthPopWindow.isShowing()) {
                                setDrwImage(binding.tvClass3, 0);
                            }
                        }
                        if (null != lengthDatas && lengthDatas.size() > 0) {
                            lengthPopWindow.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getStrLengthOption();
                        }
                        break;
                    case 1:
                        if (!isChange7) {
                            if (!onlyPopWindow.isShowing()) {
                                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass3, 0);
                            }
                        } else {
                            if (!onlyPopWindow.isShowing()) {
                                setDrwImage(binding.tvClass3, 0);
                            }
                        }
                        if (null != onlyDatas && onlyDatas.size() > 0) {
                            onlyPopWindow.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getIsOnlyOption();
                        }
                        break;
                    case 2:
                        if (!isChange10) {
                            if (!patentStatePopWindow.isShowing()) {
                                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass3, 0);
                            }
                        } else {
                            if (!patentStatePopWindow.isShowing()) {
                                setDrwImage(binding.tvClass3, 0);
                            }
                        }
                        if (null != patentStateDatas && patentStateDatas.size() > 0) {
                            patentStatePopWindow.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getPatentStatus();
                        }
                        break;
                }
                break;
            case R.id.tv_class4:
                switch (position) {
                    case 0:

                    case 1:
                        break;
                    case 2:
                        if (!isChange11) {
                            if (!pricePopWindow3.isShowing()) {
                                setTextViewBg(binding.tvClass4, R.drawable.search_bg, R.mipmap.icon_down_red, R.color.color_main);
                                setDrwImage(binding.tvClass4, 0);
                            }
                        } else {
                            if (!pricePopWindow3.isShowing()) {
                                setDrwImage(binding.tvClass4, 0);
                            }
                        }
                        if (null != priceDatas3 && priceDatas3.size() > 0) {
                            pricePopWindow3.showPopwindow(binding.roomLayout, 0, 0);
                        } else {
                            model.getPriceOption(2);
                        }
                        break;
                }
                break;
            case R.id.tv_more_choose:
                switch (position) {
                    case 0:
                        if (null != priceDatas1 && priceDatas1.size() > 0) {
                            tradeMoreWindow.showPopwindowRight(binding.roomLayout);
                        } else {
                            model.getPriceOptionByNo(4);
                        }
                        break;
                    case 1:
                        break;
                    case 2:
                        if (null != priceDatas3 && priceDatas3.size() > 0) {
                            patentMoreWindow.showPopwindowRight(binding.roomLayout);
                        } else {
                            model.getPriceOptionByNo(5);
                        }
                        break;
                }

                break;
            default:
                break;
        }
    }


    public void showBrandPopWindow(List<IpPickSelectEntity> brandDatas) {
        this.brandDatas = brandDatas;
        brandPopWindow.showPopwindow(binding.roomLayout, 0, 0);
        brandPopWindow.setData(brandDatas);
        List<String> categoryList = new ArrayList<>();
        for (IpPickSelectEntity entity : brandDatas) {
            categoryList.add(entity.getValue());
        }
        if (!TextUtils.isEmpty(category) && categoryList.size() > 0) {
            brandPopWindow.setDefItem(categoryList.indexOf(category));
        }
    }

    public void showPricePopWindow1(List<IpPickSelectEntity> priceDatas1) {
        this.priceDatas1 = priceDatas1;
        tradeMoreWindow.showPopwindowRight(binding.roomLayout);
        tradeMoreWindow.setPriceData(priceDatas1);
    }

    public void showMakeupPopWindow(List<IpPickSelectEntity> makeupDatas) {
        this.makeupDatas = makeupDatas;
        makeupPopWindow.showPopwindow(binding.roomLayout, 0, 0);
        makeupPopWindow.setData(makeupDatas);
    }

    public void showLengthPopWindow(List<IpPickSelectEntity> lengthDatas) {
        this.lengthDatas = lengthDatas;
        lengthPopWindow.showPopwindow(binding.roomLayout, 0, 0);
        lengthPopWindow.setData(lengthDatas);
    }

    public void showCopyrightPopWindow(List<IpPickSelectEntity> copyrightDatas) {
        this.copyrightDatas = copyrightDatas;
        copyrightPopWindow.showPopwindow(binding.roomLayout, 0, 0);
        copyrightPopWindow.setData(copyrightDatas);
        List<String> categoryList = new ArrayList<>();
        for (IpPickSelectEntity entity : copyrightDatas) {
            categoryList.add(entity.getValue());
        }
        if (!TextUtils.isEmpty(category) && categoryList.size() > 0) {
            copyrightPopWindow.setDefItem(categoryList.indexOf(category));
        }
    }

    public void showPricePopWindow2(List<IpPickSelectEntity> priceDatas2) {
        List<IpPickSelectEntity> listChange = new ArrayList<>();
        listChange = PickPriceUtils.showPriceList(priceDatas2);
        this.priceDatas2 = listChange;
        pricePopWindow2.showPopwindow(binding.roomLayout, 0, 0);
        pricePopWindow2.setData(listChange);
    }

    public void showOnlyPopWindow(List<IpPickSelectEntity> onlyDatas) {
        this.onlyDatas = onlyDatas;
        onlyPopWindow.showPopwindow(binding.roomLayout, 0, 0);
        onlyPopWindow.setData(onlyDatas);
    }

    public void showTradePopWindow(List<IpPickSelectEntity> tradeDatas) {
        this.tradeDatas = tradeDatas;
        tradePopWindow.showPopwindow(binding.roomLayout, 0, 0);
        tradePopWindow.setData(tradeDatas);
    }

    public void showPatentTypePopWindow(List<IpPickSelectEntity> patentTypeDatas) {
        this.patentTypeDatas = patentTypeDatas;
        patentTypePopWindow.showPopwindow(binding.roomLayout, 0, 0);
        patentTypePopWindow.setData(patentTypeDatas);
        List<String> categoryList = new ArrayList<>();
        for (IpPickSelectEntity entity : patentTypeDatas) {
            categoryList.add(entity.getId());
        }
        if (!TextUtils.isEmpty(category) && categoryList.size() > 0) {
            patentTypePopWindow.setDefItem(categoryList.indexOf(category));
        }
    }

    public void showPatentStatePopWindow(List<IpPickSelectEntity> patentStateDatas) {
        this.patentStateDatas = patentStateDatas;
        patentStatePopWindow.showPopwindow(binding.roomLayout, 0, 0);
        patentStatePopWindow.setData(patentStateDatas);
    }

    public void showPricePopWindow3(List<IpPickSelectEntity> priceDatas3) {
        this.priceDatas3 = priceDatas3;
        patentMoreWindow.showPopwindowRight(binding.roomLayout);
        patentMoreWindow.setPriceData(priceDatas3);
    }

    private void setDrwImage(TextView tv, int type) {
        Drawable img_off;
        Resources res = getResources();
        if (type == 0) {
            img_off = res.getDrawable(R.mipmap.icon_up_red);
        } else if (type == 1) {
            img_off = res.getDrawable(R.mipmap.icon_down_red);
        } else {
            img_off = res.getDrawable(R.mipmap.ic_down_arrow_n);
        }
        img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
        tv.setCompoundDrawables(null, null, img_off, null); //设置右图标
    }

    private void setTextViewBg(TextView textView, int bg, int img, int textColor) {
        textView.setBackground(getResources().getDrawable(bg));
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable_n, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_INTELL_PROP_FRG_JUMP) {
            getData();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(SearchEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_SERVICE_SEARCH) {
            String searchParam = (String) event.getMessageData();
            switch (position) {
                case 0:
                    map.put("searchParam", searchParam);
                    break;
                case 1:
                    mapCopyright.put("searchParam", searchParam);
                    break;
                case 2:
                    mapPatent.put("searchParam", searchParam);
                    break;
            }
            getData();
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(LeaseEvent event){
        if (event.getMessageTag() == EventConstant.EVENT_CREATE_MAKEOVERORDER_SECESS) {

            getData();
        }
    }
}
